

export interface Label {
    /**
     * 该标签的唯一id
     */
    id: string,

    /**
     * 标签的类型
     */
    type: "radio" | "area" | "keypoint"

    /**
     * 该标签人可读的名称
     */
    name: string,

    /**
     * 该标签在标注时使用的颜色
     */
    color: string,

    /**
     * 标注涉及的值
     */
    value?: string,
}

export interface LabelGroup {

    /**
     * 该标签组的唯一id
     */
    id: string,

    /**
     * 该标签组对外的名称
     */
    name: string,

    /**
     * 标签组
     */
    type: "group",

    domain?: "global" | "frame",

    gradio: boolean,

    labels: Array<Label>,
}

export interface View {
    name: string
    label: string
    schema: LabelGroup[]
}

const areas: Omit<Label, "type">[] = [
    {
        id: "LV",
        name: '左心室',
        color: '#8DD3C7',
    },
    {
        id: "RV",
        name: '右心室',
        color: '#FFFFB3',
    },
    {
        id: "LA",
        name: "左心房",
        color: '#BEBADA',
    },
    {
        id: "RA",
        name: '右心房',
        color: '#FB8072',
    },
    {
        id: "IVS",
        name: "室间隔",
        color: "#80B1D3",
    },
    {
        id: "IAS",
        name: "房间隔",
        color: "#FDB462",
    },
    {
        id: "FX",
        name: "前交界",
        color: "#B3DE69",
    },
    {
        id: "BX",
        name: "后交界",
        color: "#FCCDE5",
    },
    {
        id: "A1",
        name: '前叶1区',
        color: '#D9D9D9',
    },
    {
        id: "A2",
        name: '前叶2区',
        color: '#BC80BD',
    },
    {
        id: "A3",
        name: '前叶3区',
        color: '#CCEBC5',
    },
    {
        id: "P3",
        name: "后叶3区",
        color: '#FFED6F'
    },
    {
        id: "P2",
        name: "后叶2区",
        color: '#8E0152'
    },
    {
        id: "P1",
        name: "后叶1区",
        color: '#C51B7D'
    },
    {
        id: "LAA",
        name: "左心耳",
        color: '#DE77AE'
    },
    {
        id: "AV",
        name: "主动脉瓣",
        color: '#F1B6DA',
    },
    {
        id: "MV",
        name: "二尖瓣",
        color: "#C2A5CF"
    },
    {
        id: "AA",
        name: "升主动脉",
        color: '#FDE0EF',
    },
    {
        id: "DA",
        name: "降主动脉",
        color: '#F7F7F7',
    },
    {
        id: "PV1",
        name: '肺静脉1',
        color: '#E6F5D0',
    },
    {
        id: "PV2",
        name: '肺静脉2',
        color: '#B8E186',
    },
    {
        id: "PV3",
        name: '肺静脉3',
        color: '#7FBC41',
    },
    {
        id: "PV4",
        name: '肺静脉4',
        color: '#4D9221',
    },
    {
        id: "MR",
        name: "反流",
        color: '#276419',
    },
]

const keypoints: Omit<Label, 'type'>[] = [
    {
        id: "K1",
        name: "关键点1",
        color: '#A6CEE3'
    },
    {
        id: "K2",
        name: "关键点2",
        color: '#1F78B4'
    },
    {
        id: "K3",
        name: "关键点3",
        color: '#B2DF8A'
    },
    {
        id: "K4",
        name: "关键点4",
        color: '#33A02C'
    },
    {
        id: "K5",
        name: "关键点5",
        color: '#FB9A99'
    },
    {
        id: "K6",
        name: "关键点6",
        color: '#E31A1C'
    },
    {
        id: "K7",
        name: "关键点7",
        color: '#FDBF6F'
    },
    {
        id: "K8",
        name: "关键点8",
        color: '#FF7F00'
    },
    {
        id: "K9",
        name: "关键点9",
        color: '#CAB2D6'
    },
    {
        id: "K10",
        name: "关键点10",
        color: '#6A3D9A'
    },
    {
        id: "K11",
        name: "关键点11",
        color: '#FFFF99'
    },
]

const ALL_AREA: Map<string, Label> = new Map(areas.map(a => [a.id, { type: 'area', ...a }]))

console.assert(ALL_AREA.size === areas.length, "duplicate name area labels")

const ALL_KEYPOINT: Map<string, Label> = new Map(keypoints.map(a => [a.id, { type: 'keypoint', ...a }]))

console.assert(ALL_KEYPOINT.size === keypoints.length, "duplicate name area labels")

for (const [k, v] of ALL_KEYPOINT) {
    console.assert(!ALL_AREA.has(k), `duplicate id ${k} between area and keypoint`)
}

const _allnames = new Set
const _allcolor = new Set
for (const [k, v] of ALL_AREA) {
    console.assert(!_allnames.has(v.name), `duplicate name ${k} in area`)
    _allnames.add(v.name)
    console.assert(!_allcolor.has(v.color), `duplicate color ${k} in area`)
    _allcolor.add(v.color)
}

for (const [k, v] of ALL_KEYPOINT) {
    console.assert(!_allnames.has(v.name), `duplicate name ${k} in keypoint`)
    _allnames.add(v.name)
    console.assert(!_allcolor.has(v.color), `duplicate color ${k} in keypoint`)
    _allcolor.add(v.color)
}

function genGroup(groupID: string, name: string, labelIDs: string[]): LabelGroup {
    const allID = new Set
    const found: Label[] = []
    for (const id of labelIDs) {
        console.assert(!allID.has(id), `duplicate id ${id}`)
        const out = ALL_AREA.get(id) ?? ALL_KEYPOINT.get(id)
        console.assert(out !== undefined, `unknown id ${id}`)
        found.push(out!)
    }
    return {
        id: groupID,
        type: 'group',
        name: name,
        gradio: false,
        labels: found,
    }
}


const QUALITY_GROUP: LabelGroup = {
    id: 'q',
    name: '质量标签',
    type: 'group',
    domain: 'global',
    gradio: true,
    labels: [
        {
            id: 'FQ1',
            type: 'radio',
            name: '不可评估',
            color: '#D7191C',
            value: '1',
        },
        {
            id: 'FQ2',
            type: 'radio',
            name: '差',
            color: 'darkorange',
            value: '2',
        },
        {
            id: 'FQ3',
            type: 'radio',
            name: '一般',
            color: "darkgoldenrod",
            value: '3',
        },
        {
            id: 'FQ4',
            type: 'radio',
            name: '良好',
            color: 'darycyan',
            value: '4',
        },
        {
            id: 'FQ5',
            type: 'radio',
            name: '优秀',
            color: 'darkgreen',
            value: '5',
        },
    ]
}

const FRMAE_GROUP: LabelGroup = {
    id: 't',
    name: '帧标签',
    type: 'group',
    domain: 'frame',
    gradio: true,
    labels: [
        {
            id: 'SSMQ',
            type: 'radio',
            name: '收缩末期',
            color: 'darkorange',
            value: 'SSMQ'
        },
        {
            id: 'SZMQ',
            type: 'radio',
            name: '舒张末期',
            color: 'purple',
            value: 'SZMQ'
        },
        {
            id: 'SPEC',
            type: 'radio',
            name: '特殊时期',
            color: 'black',
            value: 'INPUT'
        },
    ],
}

const COMMON_GROUP: LabelGroup = {
    id: 'c',
    name: '区域',
    type: 'group',
    labels: [
        {
            id: 'testarea',
            name: '测试区域',
            type: 'area',
            color: 'green',
        }
    ],
    gradio: false,
}

const KEYPOINT_GROUP: LabelGroup = {
    id: 'k',
    name: '关键点',
    type: 'group',
    gradio: false,
    labels: [
        {
            id: 'testp',
            name: '测试点',
            type: 'keypoint',
            color: 'purple',
        }
    ],
}


const all_groups = [
    QUALITY_GROUP,
    FRMAE_GROUP,
    COMMON_GROUP,
    KEYPOINT_GROUP,
]

const views: View[] = [
    {
        name: "4CV",
        label: "四腔心",
        schema: [
            QUALITY_GROUP,
            FRMAE_GROUP,
            genGroup("c", '区域', [
                'LV',
                'LA',
                'A2',
                'P2',
                'IVS',
                'IAS',
                'RV',
                'RA',
                'LAA',
                'PV1',
                'PV2',
                'PV3',
                'PV4',
            ]),
            genGroup('k', '关键点', [
                "K1", "K2", "K3"
            ])
        ],
    },
    {
        name: "3CV",
        label: "三腔心",
        schema: [
            QUALITY_GROUP,
            FRMAE_GROUP,
            genGroup("c", '区域', [
                'LV',
                'LA',
                'A2',
                'P2',
                "AV",
                "RV",
                "AA",
                "DA",
            ]),
            genGroup('k', '关键点', [
                "K1", "K2", "K3"
            ])
        ],
    },
    {
        name: "2CV",
        label: "两腔心",
        schema: [
            QUALITY_GROUP,
            FRMAE_GROUP,
            genGroup("c", '区域', [
                'LV',
                'LA',
                'LAA',
                'A2',
                'P1',
                'P2',
                'P3',
                'PV1',
                'PV2',
                'PV3',
                'PV4',
            ]),
            genGroup('k', '关键点', [
                "K1", "K2", "K3", "K4", "K5", "K6"
            ])
        ],
    },
    {
        name: "SAX",
        label: "短轴",
        schema: [
            QUALITY_GROUP,
            FRMAE_GROUP,
            genGroup("c", '区域', [
                'LV',
                'FX',
                'BX',
                'RV',
                'IVS',
                'LAA',
                'A1',
                'A2',
                'A3',
                'P1',
                'P2',
                'P3',
            ]),
            genGroup('k', '关键点', [
                "K1", "K2", "K3", "K4", "K5", "K6", "K7", "K8", "K9", "K10", "K11"
            ])
        ],
    },
    {
        name: "4CV-COLOR",
        label: "四腔心-多普勒",
        schema: [
            QUALITY_GROUP,
            FRMAE_GROUP,
            genGroup("c", '区域', [
                'LA',
                'MR',
            ]),
        ],
    },
    {
        name: "3CV-COLOR",
        label: "三腔心-多普勒",
        schema: [
            QUALITY_GROUP,
            FRMAE_GROUP,
            genGroup("c", '区域', [
                'LA',
                'MR',
            ]),
        ],
    },
    {
        name: "2CV-COLOR",
        label: "两腔心-多普勒",
        schema: [
            QUALITY_GROUP,
            FRMAE_GROUP,
            genGroup("c", '区域', [
                'LA',
                'MR',
            ]),
        ],
    },
    {
        name: "SAX-COLOR",
        label: "短轴-多普勒",
        schema: [
            QUALITY_GROUP,
            FRMAE_GROUP,
            genGroup("c", '区域', [
                'LV',
                'MR',
                'MV',
            ]),
        ],
    },
]

console.log(JSON.stringify(views, undefined, '  '))
